package com.powernode.shop.oauth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.security.KeyPair;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/9
 */
@Configuration
public class AuthConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    private JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 非对称加密的方式
        ClassPathResource resource = new ClassPathResource("cxs-jwt.jks");
        // 创建钥匙工厂
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "cxs123".toCharArray());
        // 创建钥匙
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("cxs-jwt");
        // 设置钥匙
        jwtAccessTokenConverter.setKeyPair(keyPair);
        return jwtAccessTokenConverter;
    }

    /**
     * 配置第三方应用
     * 我们可以把前端应用都当做第三方应用
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("web")
                .secret(passwordEncoder().encode("web-secret"))
                .scopes("all")
                .authorizedGrantTypes("password") // 给我们的前端应用即管理员授权
                .accessTokenValiditySeconds(21600)
                .redirectUris("https://www.baidu.com")
                .and()
                .withClient("power")
                .secret(passwordEncoder().encode("power-secret"))
                .scopes("all")
                .authorizedGrantTypes("client_credentials") // 使用客户端授权 颁发一个应用的全局token 为了在服务之间做特殊传递的
                .accessTokenValiditySeconds(Integer.MAX_VALUE)
                .redirectUris("https://www.baidu.com");
    }

    /**
     * 1.code  先登录 在 获取code  在获取token
     * 2.静默授权 先登录 直接获取token
     * 3. password 直接获取token  里面默认做了认证的
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore())
                .accessTokenConverter(jwtAccessTokenConverter())
                .authenticationManager(authenticationManager);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
